package com.app.service.impl;

import com.app.common.*;
import com.app.mapper.JobFairCompanyMapper;
import com.app.model.*;
import com.app.service.*;
import com.app.vo.JobFairCompanyVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * @Description:招聘会
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class JobFairCompanyServiceImpl implements JobFairCompanyService {
    @Autowired
    JobFairCompanyMapper JobFairCompanyMapper;

    @Autowired
    EnterpriseService enterpriseService;

    @Autowired
    JobFairCompanyPostService jobFairCompanyPostService;
    @Autowired
    PositionService positionService;
    @Autowired
    JobPublishService jobPublishService;
    @Autowired
    LookMeService lookMeService;
    @Autowired
    CustomerService customerService;
    @Autowired
    UserService userService;
    @Autowired
    WorkApplyService workApplyService;

    @Autowired
    IndustryService industryService;

    @Override
    public JobFairCompanyModel getModelBySidAndCid(int sId, int cId) {
        QueryWrapper<JobFairCompanyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("sid", sId);
        queryWrapper.select().eq("cid", cId);
        List<JobFairCompanyModel> userList = JobFairCompanyMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public int insert(JobFairCompanyModel model) {
        return JobFairCompanyMapper.insert(model);
    }

    @Override
    public List<JobFairCompanyModel> getModelBySid(int sId) {
        QueryWrapper<JobFairCompanyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("sid", sId);
        List<JobFairCompanyModel> userList = JobFairCompanyMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public Object getEnterpriseByFair(int sId) {

        List<JobFairCompanyModel> jobFairCompanyList = getModelBySid(sId);
        List<EnterpriseModel> enterpriseList = new ArrayList<>();
        if (jobFairCompanyList != null) {
            for (int i = 0; i < jobFairCompanyList.size(); i++) {
                EnterpriseModel enterprise;
                enterprise = enterpriseService.findModelById(jobFairCompanyList.get(i).getCid());
                int fairCount = jobFairCompanyPostService.countBySidAndCid(sId, enterprise.getId());
                enterprise.setFairCount(fairCount);
                for (int j = 0; j < ScaleTool.SCALE_VO_LIST.size(); j++) {
                    if (enterprise.getScaleId() == ScaleTool.SCALE_VO_LIST.get(j).getId()) {
                        enterprise.setScaleName(ScaleTool.SCALE_VO_LIST.get(j).getData());
                    }
                }
                IndustryModel industryModel = industryService.getModelByCode(enterprise.getEnterpriseType());
                enterprise.setEnterpriseTypeText(industryModel.getIndustryName());
                enterpriseList.add(enterprise);
                String fairPostName = enterprise.getEnterpriseName();
                enterprise.setFairPostName(fairPostName);
            }
        }
        return enterpriseList;
    }

    @Override
    public Object getPublishByFair(int sId) {


        List<JobFairCompanyPostModel> jobFairCompanyPostList = jobFairCompanyPostService.getListBySid(sId);
        List<JobPublishModel> jobPublishList = new ArrayList<>();
        if (jobFairCompanyPostList != null) {
            for (int i = 0; i < jobFairCompanyPostList.size(); i++) {
                JobPublishModel jobPublish = jobPublishService.findAllJobById(jobFairCompanyPostList.get(i).getPid());
                Integer expId = jobPublish.getExpId();
                Integer eduId = jobPublish.getEduId();
                if (expId != null) {
                    for (int j = 0; j < ExperienceTool.EXPERIENCE_VO_LIST.size(); j++) {
                        if (expId == ExperienceTool.EXPERIENCE_VO_LIST.get(j).getId()) {
                            if (expId == 1 || expId == 6) {
                                jobPublish.setExpRequireText(ExperienceTool.EXPERIENCE_VO_LIST.get(j).getData());
                            } else {
                                jobPublish.setExpRequireText(ExperienceTool.EXPERIENCE_VO_LIST.get(j).getData() + "年");
                            }

                        }
                    }
                }
                if (eduId != null) {
                    for (int j = 0; j < EducationTool.EDUCATION_VO_LIST.size(); j++) {
                        if (eduId == EducationTool.EDUCATION_VO_LIST.get(j).getId()) {
                            jobPublish.setMinEducationText(EducationTool.EDUCATION_VO_LIST.get(j).getData());
                        }
                    }
                }

                jobPublishList.add(jobPublish);
            }
        }
        return jobPublishList;
    }

    @Override

    public Object getEnterpriseDetails(int sId, int cId) {
        EnterpriseModel enterprise;
        enterprise = enterpriseService.findModelById(cId);
        for (int j = 0; j < ScaleTool.SCALE_VO_LIST.size(); j++) {
            if (enterprise.getScaleId() == ScaleTool.SCALE_VO_LIST.get(j).getId()) {
                enterprise.setScaleName(ScaleTool.SCALE_VO_LIST.get(j).getData());
            }
        }
        IndustryModel industryModel = industryService.getModelByCode(enterprise.getEnterpriseType());
        enterprise.setEnterpriseTypeText(industryModel.getIndustryName());
        CustomerModel cus = customerService.getCustomerById(cId);
        List<JobFairCompanyPostModel> jobFairCompanyPostList = jobFairCompanyPostService.getListBySidAndCid(sId, cId);
        List<JobPublishModel> jobPublishList = new ArrayList<>();
        for (int i = 0; i < jobFairCompanyPostList.size(); i++) {
            JobPublishModel jobPublish = jobPublishService.findAllJobById(jobFairCompanyPostList.get(i).getPid());
            Integer expId = jobPublish.getExpId();
            Integer eduId = jobPublish.getEduId();
            if (expId != null) {
                for (int j = 0; j < ExperienceTool.EXPERIENCE_VO_LIST.size(); j++) {
                    if (expId == ExperienceTool.EXPERIENCE_VO_LIST.get(j).getId()) {
                        if (expId == 1 || expId == 6) {
                            jobPublish.setExpRequireText(ExperienceTool.EXPERIENCE_VO_LIST.get(j).getData());
                        } else {
                            jobPublish.setExpRequireText(ExperienceTool.EXPERIENCE_VO_LIST.get(j).getData() + "年");
                        }

                    }
                }
            }
            if (eduId != null) {
                for (int j = 0; j < EducationTool.EDUCATION_VO_LIST.size(); j++) {
                    if (eduId == EducationTool.EDUCATION_VO_LIST.get(j).getId()) {
                        jobPublish.setMinEducationText(EducationTool.EDUCATION_VO_LIST.get(j).getData());
                    }
                }
            }

            jobPublishList.add(jobPublish);
        }
        enterprise.setJobPublishList(jobPublishList);
        LookMeModel lookMe2 = lookMeService.findRidAndPid(0, 0, enterprise.getId(), cus.getId(), 1, 2);//查询关注状态
        if (lookMe2 != null) {
            enterprise.setFollow(1);
        } else {
            enterprise.setFollow(0);
        }


        return null;
    }

    @Override
    public int countBySid(int sId) {
        QueryWrapper<JobFairCompanyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("sid", sId);
        int count = JobFairCompanyMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public int countByCid(int sId) {
        QueryWrapper<JobFairCompanyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT cid").eq("sid", sId);
        int count = JobFairCompanyMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public int countByResume(int sId) {
        List<JobFairCompanyModel> jobFairCompanyList = getModelBySid(sId);
        int count = 0;
        if (jobFairCompanyList != null) {
            for (int i = 0; i < jobFairCompanyList.size(); i++) {
                EnterpriseModel enterprise = enterpriseService.findModelById(jobFairCompanyList.get(i).getCid());
                int workApplyCCount = workApplyService.enterpriseWorkApplyCount(enterprise.getId());
                count += workApplyCCount;
            }
        }

        return count;
    }

    @Override
    public List<JobFairCompanyModel> getLIstByModel(JobFairCompanyModel model) {
        return JobFairCompanyMapper.getLIstByModel(model);
    }

    @Override
    public int getCountByModel(JobFairCompanyModel model) {
        return JobFairCompanyMapper.getCountByModel(model);
    }

    @Override
    public int getCountByEx() {
        QueryWrapper<JobFairCompanyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("audit_state", 3);
        return JobFairCompanyMapper.selectCount(queryWrapper);
    }

    @Override
    public int countAll() {
        QueryWrapper<JobFairCompanyModel> queryWrapper = new QueryWrapper<>();
        return JobFairCompanyMapper.selectCount(queryWrapper);
    }

    @Override
    public JobFairCompanyVo jobFairList(JobFairCompanyModel model) throws ParseException {

        List<JobFairCompanyModel> modelList = getLIstByModel(model);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat DATA_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < modelList.size(); i++) {
            String bir = sdf.format(DATA_FORMAT.parse(modelList.get(i).getCrtTime()));
            modelList.get(i).setCrtTimeText(bir);
            for (int j = 0; j < ScaleTool.SCALE_VO_LIST.size(); j++) {
                if (modelList.get(i).getScaleId() == ScaleTool.SCALE_VO_LIST.get(j).getId()) {
                    modelList.get(i).setScaleText(ScaleTool.SCALE_VO_LIST.get(j).getData());
                }
            }
            IndustryModel industryModel = industryService.getModelByCode(modelList.get(i).getJobType());
            modelList.get(i).setJobTypeText(industryModel.getIndustryName());


            if (modelList.get(i).getAuditState()==1) {
                modelList.get(i).setAuditStateText("通过");
            }else if(modelList.get(i).getAuditState()==2)
            {
                modelList.get(i).setAuditStateText("未通过");
            }else
            {
                modelList.get(i).setAuditStateText("待审核");
            }
        }
        int count = getCountByModel(model);
        JobFairCompanyVo vo = new JobFairCompanyVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }

    @Override
    public List<JobFairCompanyModel> getMyJobFair(JobFairCompanyModel model) {
        return JobFairCompanyMapper.getMyJobFair(model);
    }

    @Override
    public JobFairCompanyModel getModelById(int id) {
        QueryWrapper<JobFairCompanyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("id", id);
        JobFairCompanyModel model = JobFairCompanyMapper.selectOne(queryWrapper);
        return model;
    }

    @Override
    public void update(JobFairCompanyModel model) {
        JobFairCompanyMapper.updateById(model);
    }

}
